import os
import datetime

# Run the unit-tests first
print "\nRUNNING UNIT TESTS\n----------------------------------------------------------------------"

def getAllTestFiles(dirname=".", recursive=True):
	tests = []
	files = os.listdir(dirname)
	for f in files:
		filename = os.path.join(dirname, f)
		if os.path.isfile(filename):
			if f.endswith("_test.py"):
				tests.append(filename)
		if os.path.isdir(filename):
			if recursive:
				tests = tests + getAllTestFiles(dirname=filename)
	return tests

unitTestFailed = False
testCases = getAllTestFiles()
for testCase in testCases:
	print "\nRUNNING TESTCASE " + testCase + "\n----------------------------------------------------------------------"
	unitTestFailed = True if os.system("python " + testCase) != 0 else False

if not unitTestFailed:
	# Create the static version file

	d = datetime.datetime.today()
	ts = str(d.day) + str(d.month) + str(d.year) + str(d.hour) + str(d.minute)
	print "\nCREATING THE STATIC FILE VERSIONS FILE (" + ts + ")\n----------------------------------------------------------------------"
	fileObj = open("staticTs.txt", "w")
	fileObj.write(ts)
	fileObj.close()

	# Upload to google app engine

	print "\nUPLOADING TO PROD\n----------------------------------------------------------------------"
	os.system("appcfg.py update .")